package com.dxy.yzs.marathon.controller;


import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.dxy.yzs.marathon.common.R;
import com.dxy.yzs.marathon.common.log.annotation.LogAnnotation;
import com.dxy.yzs.marathon.param.NewMsgParam;
import com.dxy.yzs.marathon.service.MessageService;
import com.dxy.yzs.marathon.vo.MessageContactVO;
import com.dxy.yzs.marathon.vo.MessageVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
public class MessageController {
    @Autowired
    private MessageService messageService;

    @LogAnnotation(module = "msg")
    @PostMapping(path = "/sendMsg")
    public R sendMsg(
            @RequestBody NewMsgParam param
    ) {
        MessageVO messageContent = messageService.sendNewMsg(param.getSenderUid(), param.getRecipientUid(), param.getContent(), param.getMsgType());
        return R.ok().put("data",messageContent);
    }

    @LogAnnotation(module = "msg")
    @GetMapping(path = "/queryMsg")
    public R queryMsg(@RequestParam Long ownerUid, @RequestParam Long otherUid) {
        List<MessageVO> messageVO = messageService.queryConversationMsg(ownerUid, otherUid);
        return R.ok().put("data",messageVO);
    }

    @LogAnnotation(module = "msg")
    @GetMapping(path = "/queryMsgSinceMid")
    public R queryMsgSinceMid(@RequestParam Long ownerUid, @RequestParam Long otherUid, @RequestParam Long lastMid) {
        List<MessageVO> messageVO = messageService.queryNewerMsgFrom(ownerUid, otherUid, lastMid);
       return R.ok().put("data",messageVO);
    }

    @LogAnnotation(module = "msg")
    @GetMapping(path = "/queryContacts")
    public R queryContacts(@RequestParam Long ownerUid) {
        MessageContactVO contactVO = messageService.queryContacts(ownerUid);
        return R.ok().put("data",contactVO);
    }
}
